﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcContrib.Pagination;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class SoccerTeamController : Controller
    {
        #region "private"
        private readonly IUserRepository _userRepository;
        private readonly IDepositRepository _depositRepository;
        private readonly IWithDrawRepository _withDrawRepository;
        private readonly IUserService _userService;
        private readonly ISoccerTeamRepository _soccerTeamRepository;
        private readonly ICountryRepository _countryRepository;
        private readonly IContactRepository _contactRepository;        
        private readonly IStoreSecureRepositoty _storeSecureRepositoty;

        public SoccerTeamController(
            IUserRepository userRepository,
            IDepositRepository depositRepository,
            IWithDrawRepository withDrawRepository,
            IUserService userService,
            ISoccerTeamRepository soccerTeamRepository,
            ICountryRepository countryRepository,
            IContactRepository contactRepository,            
            IStoreSecureRepositoty storeSecureRepositoty)
        {
            _userRepository = userRepository;
            _depositRepository = depositRepository;
            _withDrawRepository = withDrawRepository;
            _userService = userService;
            _soccerTeamRepository = soccerTeamRepository;
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;            
            _storeSecureRepositoty = storeSecureRepositoty;
        }

        #endregion              

        #region "LoadAjaxContent"

        public  ActionResult Index()
        {
            return View();
        }
        public ActionResult LoadAjax(int page)
        {
            var listTeam = _soccerTeamRepository.GetMany(null,
                                                             d =>
                                                             d.OrderByDescending(t => t.Status).ThenBy(t => t.TeamName));
            ViewBag.result = listTeam.Count();
            if (page == 1)
            {
                ViewBag.start = 0;
            }
            else
            {
                ViewBag.start = (page - 1) * 10;
            }
            return PartialView("_MainContent", listTeam.AsPagination(page, 10));
        }
        #endregion
        
        #region "UpdateStatus"
        public ActionResult UpdateStatus(int id, int status)
        {
            var wd = _soccerTeamRepository.GetById(id);
            if (status == 1)
            {
                wd.Status = false;
                _soccerTeamRepository.SaveChanges();
                ViewBag.status = false;
                ViewBag.id = id;
                ViewBag.controller = "SoccerTeam";
                return PartialView("_UpdateStatus");
            }
            wd.Status = true;
            _soccerTeamRepository.SaveChanges();
            ViewBag.status = true;
            ViewBag.id = id;
            ViewBag.controller = "SoccerTeam";
            return PartialView("_UpdateStatus");
        }

        #endregion

        #region Modify Obj
        
        public ActionResult Modify(int? id)
        {            
            if (Session["admin"] == null)
                return RedirectToAction("Login", "DashBoard");
            if (id != null)
            {
                var obj = _soccerTeamRepository.GetById((int) id);
                ViewBag.Id = id;
                return View(obj);
            }     
            return View();
        }

        [HttpPost]
        public ActionResult Modify(int? id,FormCollection obj)
        {
            var soccerName = obj["contentAdmincp"];            
            if (id != null)
            {
                var soccerTeam = _soccerTeamRepository.GetById((int) id);
                if(soccerTeam!=null)
                {
                    soccerTeam.TeamName = soccerName;
                    _soccerTeamRepository.SaveChanges();
                    return Content("1");
                }
                return Content("0");
            }
            else
            {
                var soccerTeam = new SoccerTeam()
                                     {
                                         TeamName = soccerName,
                                         Status = false,
                                         CreatedTime = DateTime.Now                                         
                                     };
                try
                {
                    _soccerTeamRepository.Add(soccerTeam);
                    _soccerTeamRepository.SaveChanges();
                    return Content("1");
                }
                catch (Exception)
                {

                    return Content("0");
                }                                
            }            
        }
        #endregion

    }
}
